import { isAuth } from '@/utils';
import { Redirect } from 'react-router-dom';
import { Route } from 'react-router-dom';

export const AuthRoute = ({ component: Component, ...rest }) => {
  return (
    <Route
      {...rest}
      render={props => {
        // 判断是否登陆来决定展示组件
        if (isAuth()) return <Component />;
        return (
          <Redirect
            // 存储登陆前的路由，方便后续重定向到登陆前的页面
            to={{ pathName: '/home', state: { from: props.location.pathname } }}
          />
        );
      }}
    ></Route>
  );
};
